package com.qbkj.base_lib.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.qbkj.base_lib.R;
import com.qbkj.base_lib.utils.ViewController;
import com.qbkj.base_lib.utils.TagUtils;


/**
 * Created by St on 2018/4/12.
 */

public abstract class BaseDialog extends Dialog {
    protected View mView;
    protected Context mContext;
    protected Intent mIntent;
    protected ViewController g;
    protected Bundle mBundle;
    private Activity activity;

    public BaseDialog(@NonNull Context context) {
        super(context, R.style.UniversalDialogStyle);
        this.mContext = context;
        this.activity= (Activity) context;
        this.mIntent = new Intent();
        this.mBundle = new Bundle();
        mView = LayoutInflater.from(context).inflate(onCreateLayout(), null, false);
        setContentView(mView);
        g = new ViewController(mContext,mView);
        setCanceledOnTouchOutside(false);//击屏幕，dialog不消失；点击物理返回键dialog消失
    }


    protected abstract int onCreateLayout();

    protected void skipActivity(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode, mIntent, mBundle);
    }

    protected void skipActivity(Class cls) {
        skipActivity(cls, TagUtils.NORESULTCODE, mIntent, mBundle);
    }

    protected void skipActivityFinish(Class cls) {
        skipActivity(cls);
        activity.finish();
    }

    protected void skipActivityFinish(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode);
        activity.finish();
    }

    //跳转
    public void skipActivity(Class cls, int ResultCode, Intent intent, Bundle bundle) {
        intent.setClass(activity, cls);
        if (bundle != null) intent.putExtras(bundle);
        if (TagUtils.NORESULTCODE != ResultCode) {
            activity.startActivityForResult(intent, ResultCode);
        } else {
            activity.startActivity(intent);
        }
    }

    protected void toast(String text) {
        if (text != null) {
            Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 居中显示
     */
    protected void initCenterLayout() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
    }

    /**
     * 底部显示Dialog
     */
    protected void initBottomLayout() {
        Window dialogWindow = getWindow();
        dialogWindow.setWindowAnimations(R.style.dialog_animation);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
    }
}
